package com.base.mail.utils;

import android.text.TextUtils;


import com.base.mail.adapter.IAdapterIndex;
import com.base.mail.bean.ContactEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;


/**
 * Created by jp on 2017/12/5.
 */

public class LetterIndexer implements IAdapterIndex<List<ContactEntity>> {


    private List<String> mSections = new ArrayList();
    private List<Integer> mPositions = new ArrayList();
    private int mCount;
    private List<ContactEntity> mContactList;



    public LetterIndexer(List<ContactEntity> list)
    {
        mContactList=list;
        init();
    }




    private void init() {
        if(this.mContactList != null) {
            this.mCount = this.mContactList.size();
            String lastString = null;

            for(int i = 0; i < this.mCount; ++i) {
                String[] names = ((ContactEntity)this.mContactList.get(i)).getShortNames();
                String thisString;
                if(names == null) {
                    thisString = "#";
                    if(!thisString.equals(lastString)) {
                        this.mSections.add(thisString);
                        this.mPositions.add(Integer.valueOf(i));
                        lastString = thisString;
                    }
                } else {
                    thisString = null;
                    if(names.length != 0 && !TextUtils.isEmpty(names[0]) && ContractUtils.isEnglishOnly(names[0].charAt(0))) {
                        thisString = names[0].substring(0, 1).toUpperCase(Locale.getDefault());
                    } else {
                        thisString = "#";
                    }

                    if(!thisString.equals(lastString)) {
                        this.mSections.add(thisString);
                        this.mPositions.add(Integer.valueOf(i));
                        lastString = thisString;
                    }
                }
            }
        }

    }




    public int getSectionForItem(String alpha) {
        return this.mSections.indexOf(alpha);
    }











    @Override
    public void updateIndexer() {
        this.mSections = new ArrayList();
        this.mPositions = new ArrayList();
        this.mCount = 0;
        this.init();
    }

    @Override
    public void clearIndexr() {
        this.mSections.clear();
        this.mPositions.clear();
        this.mCount = 0;
    }

    @Override
    public void updateIndexer(List<ContactEntity> model) {
        this.mContactList = model;
        this.updateIndexer();

    }

    @Override
    public Object[] getSections() {
        return this.mSections.toArray();
    }

    @Override
    public int getPositionForSection(int section) {
        return section >= 0 && section < this.mSections.size()?((Integer)this.mPositions.get(section)).intValue():-1;
    }

    @Override
    public int getSectionForPosition(int position) {
        if(position >= 0 && position < this.mCount) {
            int index = this.mPositions.indexOf(Integer.valueOf(position));
            return index >= 0?index:-index - 2;
        } else {
            return -1;
        }
    }
    public List<Integer> getPositions()
    {
        return mPositions;
    }
}
